﻿using Dapper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace NewBee.Core.Data.SQLBuilder
{
    public class Insert
    {
        private DbHelper _DbHelper = null;  //数据操作类

        private string INSERT_SQL = @"INSERT INTO {0} ({1}) VALUES ({2})";  //新增语句

        private string _TableName = string.Empty;   //表名

        private Hashtable _Columns = new Hashtable();  //列名称/值 的集合

        private DynamicParameters _Parameters = new DynamicParameters();  //参数

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="dbHelper">帮助类</param>
        /// <param name="tableName">表名称</param>
        public Insert(DbHelper dbHelper, string tableName)
        {
            _DbHelper = dbHelper;
            _TableName = tableName;
        }

        /// <summary>
        /// 初始化列名称/值 集合
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        public Insert Column(string name,object value)
        {
            _Columns.Add(name, value);
            return this;
        }

        /// <summary>
        /// 初始化列名称/值 集合
        /// </summary>
        /// <param name="columns">列名称/值 集合</param>
        /// <returns></returns>
        public Insert Column(Hashtable columns)
        {
            _Columns = columns;
            return this;
        }

        /// <summary>
        /// 执行插入操作
        /// </summary>
        /// <returns></returns>
        public int Execute()
        {
            if (_Columns.Count <= 0)
                return -1;
            string columnNames = "";//列名称集合
            string columnValues = "";//值集合
            
            foreach (string key in _Columns.Keys)
            {
                columnNames += key + ",";
                columnValues += "@"+key + ",";
                _Parameters.Add("@" + key, _Columns[key]==null?0: _Columns[key]); 
            }
            string sql = string.Format(INSERT_SQL, _TableName, columnNames.TrimEnd(','), columnValues.TrimEnd(','));

            return _DbHelper.Execute(sql, _Parameters); 
        }
    }

    public class Insert_T<T>
    {
        private DbHelper _DbHelper = null;  //数据操作类

        private string INSERT_SQL = @"INSERT INTO {0} ({1}) VALUES ({2})";  //新增语句

        private string _TableName = string.Empty;   //表名称

        private string _PrimaryKey = string.Empty;

        private T _Model;

        /// <summary>
        /// 参数
        /// </summary>
        private DynamicParameters _Parameters=new DynamicParameters();

        public Insert_T(DbHelper dbHelper, string tableName,T model,string PrimaryKey)
        {
            _DbHelper = dbHelper;
            _TableName = tableName;
            _Model = model;
            _PrimaryKey = PrimaryKey;
        }

        
        /// <summary> 
        /// 添加 
        /// </summary> 
        /// <typeparam name="T"></typeparam> 
        /// <param name="model"></param> 
        /// <returns></returns> 
        private string InsertSQL(T model)
        {
            StringBuilder sbColumns = new StringBuilder();
            StringBuilder sbValues = new StringBuilder();

            PropertyInfo[] _PropertyInfos = model.GetType().GetProperties();

            foreach (PropertyInfo _PropertyInfo in _PropertyInfos)
            {
                if (null != _PropertyInfo.GetValue(model, null))
                {
                    if (_PropertyInfo.Name!=_PrimaryKey)
                    {
                        sbColumns.Append(_PropertyInfo.Name + ",");
                        sbValues.Append("@").Append(_PropertyInfo.Name).Append(",");
                        _Parameters.Add("@" + _PropertyInfo.Name, _PropertyInfo.GetValue(model));
                        
                    }
                }
            }
            if (sbColumns.Length > 0 && sbValues.Length > 0)
            {
                sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
                sbValues.Remove(sbValues.ToString().Length - 1, 1);
            }
            
            string strSql = string.Format(INSERT_SQL, _TableName, sbColumns.ToString(), sbValues.ToString());

            return strSql;
        }

        /// <summary>
        /// 执行插入操作
        /// </summary>
        /// <returns></returns>
        public int Execute()
        {

            string sql = InsertSQL(_Model);

            return _DbHelper.Execute(sql, _Parameters);
        }

    }
}
